<?php
namespace Addons\Materiallibrary\Controller\Admin;
use Lib\Response;
use Session;
use Models\BaseModel as Model;
use Addons\QyWechat\Helper;

use Addons\WechatOpenPlatform\Services\Wxopenapi;

class Material extends \BaseController
{
	//所有已经授权的公众号集合
	public function authorizer_lists(){
		$wid = \Session::get("wid");
		$list = (new Model("qy_gzh_token"))->where(["wid"=>$wid])->list_all();
		foreach ($list as $key => $value) {
			$list[$key]->message = json_decode($value->message);
		}
		if(is_post()){
			setApi();
			$sql = db_query("update qvmh_qy_gzh_token SET select_status=1");
			$list = (new Model("qy_gzh_token"))->where(["wid"=>$wid,"authorizer_appid"=>$_POST["authorizer_appid"]])->list_all();
			foreach ($list as $key => $value) {
				$value->select_status = 2;
				$value->save();
			}
			sendSuccess("成功");
		}
		$this->assign("list", $list);
		return $this->show();
	}

	//点击确定按钮，展示所选中的列表
	public function authorizer_list(){
		$wid = \Session::get("wid");
      	$h = Helper::getInstance()->get_http_server_name();
      	$image = $h."/images/lie.png";
		$where["wid"] = $wid;
		$list = (new Model("qy_gzh_token"))->where(["wid"=>$wid,"select_status"=>2])->list_all();
		foreach ($list as $key => $value) {
			$list[$key]->message = json_decode($value->message);
			$authorizer_appids[] = $value->authorizer_appid;
			if($value->update_time){
				$times[] = strtotime($value->update_time);
			}
		}
		if(count($authorizer_appids) != 0){
			if($_POST["title"]){
				$where["title@~"] = $_POST["title"];
			}
			$where["authorizer_appid"] = $authorizer_appids;
			$article_list = (new Model("qy_article"))->where($where)->list_all();
			if($_POST["title"]){
				setApi();
				sendSuccess("获取成功",[$article_list,count($article_list),$image]);
			}
		}
		if(max($times)){
			$max = date("Y-m-d H:i",max($times));
		}
		$count = count($authorizer_appids);
		$this->assign("article_list", $article_list);
		$this->assign("count", $count);
		$this->assign("image", $image);
		$this->assign("max", $max);
		$this->assign("list", $list);
		return $this->show();
	}

	//单个或者搜索时
	public function authorizer_one(){
		$wid = \Session::get("wid");
		$where["wid"] = $wid;
      	$h = Helper::getInstance()->get_http_server_name();
      	$image = $h."/images/lie.png";
		$list = (new Model("qy_gzh_token"))->where(["wid"=>$wid,"select_status"=>2])->list_all();
		foreach ($list as $key => $value) {
			$list[$key]->message = json_decode($value->message);
			$authorizer_appids[] = $value->authorizer_appid;
		}
		if(count($authorizer_appids) != 0){
			$where["authorizer_appid"] = \Request::get("authorizer_appid");
			$user = (new Model("qy_gzh_token"))->find(["wid"=>$wid,"authorizer_appid"=>\Request::get("authorizer_appid")]);
			$user->message = json_decode($user->message,1);
			$article_list = (new Model("qy_article"))->where($where)->list_all();
			if(is_post()){
				setApi();
				$user = (new Model("qy_gzh_token"))->find(["wid"=>$wid,"authorizer_appid"=>$_POST["authorizer_appid"]]);
				$wheres["title@~"] = $_POST["title"];
				$wheres["wid"] = $wid;
				$user->message = json_decode($user->message,1);
				$article_list = (new Model("qy_article"))->where($wheres)->list_all();
				sendSuccess("成功",[$article_list,count($article_list),$image]);
			}
		}
		$count = count($authorizer_appids);
		$this->assign("article_list", $article_list);
		$this->assign("count", $count);
		$this->assign("user", $user);
		$this->assign("image", $image);
		$this->assign("list", $list);
		return $this->show("Admin/Material/authorizer_list");
	}
	
	//全部同步
	public function synchronization(){
		setApi();
		$wid = \Session::get("wid");
		if($_POST["authorizer_appid"]){
			$authorizer_appid = $_POST["authorizer_appid"];
			$restus = Wxopenapi::batchget_material($authorizer_appid);
			$user = (new Model("qy_gzh_token"))->find(["wid"=>$wid,"authorizer_appid"=>$authorizer_appid]);
			$message = json_decode($user->message);
			if($restus == 'error'){
				sendError($message->authorizer_info->nick_name."同步失败");
			}
			$user->update_time = date('Y-m-d H:i:s', time());
			$user->save();
		}else{
			$users = (new Model("qy_gzh_token"))->where(["wid"=>$wid,"select_status"=>2])->list_all();
			foreach ($users as $key => $value) {
				$message = json_decode($value->message,1);
				$restus = Wxopenapi::batchget_material($value->authorizer_appid);
				if($restus == 'error'){
					sendError($message->authorizer_info->nick_name."同步失败");
				}
				$value->update_time = date('Y-m-d H:i:s', time());
				$value->save();
			}
		}
		sendSuccess("同步成功");
	}


	//单个同步
	public function synchronization_one(){
		setApi();
		$media_id = $_POST["media_id"];
		$media = (new Model("qy_article"))->find(["media_id"=>$media_id]);
		$restus = Wxopenapi::get_material($media_id,$media->authorizer_appid);
		if($restus == "scuuess"){
			sendSuccess("同步成功");
		}
		sendError("同步失败");
	}
}
?>